package cn.iocoder.yudao.module.security.controller.admin.asset;

import cn.iocoder.yudao.module.security.service.asset.SecurityAssetAggService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.security.controller.admin.asset.vo.*;
import cn.iocoder.yudao.module.security.dal.dataobject.asset.SecurityAssetDO;
import cn.iocoder.yudao.module.security.service.asset.SecurityAssetService;

@Tag(name = "管理后台 - 公司资产管理")
@RestController
@RequestMapping("/security/asset")
@Validated
public class SecurityAssetController {

    @Resource
    private SecurityAssetService assetService;

    @Resource
    private SecurityAssetAggService securityAssetAggService;

    @PostMapping("/create")
    @Operation(summary = "创建公司资产管理")
    @PreAuthorize("@ss.hasPermission('security:asset:create')")
    public CommonResult<Long> createAsset(@Valid @RequestBody SecurityAssetSaveReqVO createReqVO) {
        return success(assetService.createAsset(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公司资产管理")
    @PreAuthorize("@ss.hasPermission('security:asset:update')")
    public CommonResult<Boolean> updateAsset(@Valid @RequestBody SecurityAssetSaveReqVO updateReqVO) {
        assetService.updateAsset(updateReqVO);
        return success(true);
    }

    @PostMapping("/confirm-batch")
    @Operation(summary = "批量确认")
    @PreAuthorize("@ss.hasPermission('security:asset:update')")
    public CommonResult<Boolean> confirmAssetBatch(@Valid @RequestBody List<Long> reqVO) {
        assetService.confirmAssetBatch(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公司资产管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('security:asset:delete')")
    public CommonResult<Boolean> deleteAsset(@RequestParam("id") Long id) {
        assetService.deleteAsset(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公司资产管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('security:asset:query')")
    public CommonResult<SecurityAssetRespVO> getAsset(@RequestParam("id") Long id) {
        SecurityAssetDO asset = assetService.getAsset(id);
        return success(BeanUtils.toBean(asset, SecurityAssetRespVO.class));
    }

    @GetMapping("/agg")
    @Operation(summary = "获得公司资产管理")
    @PreAuthorize("@ss.hasPermission('security:asset:query')")
    public CommonResult<Integer> aggAsset() {
        Integer result = securityAssetAggService.aggAsset();
        return success(result);
    }

    @GetMapping("/page")
    @Operation(summary = "获得公司资产管理分页")
    @PreAuthorize("@ss.hasPermission('security:asset:query')")
    public CommonResult<PageResult<SecurityAssetRespVO>> getAssetPage(@Valid SecurityAssetPageReqVO pageReqVO) {
        PageResult<SecurityAssetDO> pageResult = assetService.getAssetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SecurityAssetRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公司资产管理 Excel")
    @PreAuthorize("@ss.hasPermission('security:asset:export')")
    @OperateLog(type = EXPORT)
    public void exportAssetExcel(@Valid SecurityAssetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SecurityAssetDO> list = assetService.getAssetPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "公司资产管理.xls", "数据", SecurityAssetRespVO.class,
                        BeanUtils.toBean(list, SecurityAssetRespVO.class));
    }

}